//package com.wd.cloud.applet.acpect;
//
//import com.alibaba.fastjson.JSONObject;
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.fasterxml.jackson.databind.SerializationFeature;
//import com.gk.cloud.retailers.common.Response;
//import com.gk.cloud.retailers.common.enums.ResponseCodeEnum;
//import com.gk.cloud.retailers.config.ApiSecretConfig;
//import com.gk.cloud.retailers.entity.commons.ApiSecretModel;
//import com.gk.cloud.retailers.utils.HelperUtils;
//import com.gk.cloud.retailers.utils.SignUtil;
//import com.wd.cloud.applet.common.Response;
//import com.wd.cloud.applet.common.enums.ResponseCodeEnum;
//import com.wd.cloud.applet.utils.HelperUtils;
//import com.wd.cloud.applet.utils.SignUtil;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//import javax.servlet.http.HttpServletRequest;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpMethod;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
///**
// * 提供给外部的接口签名
// *
// * @author hjx
// */
//
//@Slf4j
//@Aspect
//@Component
//public class ApiSignAspect {
//
//    @Autowired
//    private ApiSecretConfig apiSecretConfig;
//
//     /**
//     * 定义切入点
//     */
//
//    @Pointcut("execution(* com.gk.cloud.retailers.controller.applet..*.*(..))")
//    public void aspect() {
//    }
//
///**
//     * 签名方法环绕通知
//     *
//     * @param joinPoint 切点
//     * @return 方法反射
//     */
//
//    @Around(value = "aspect()")
//    public Object doSignAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        JSONObject params = getRequestParams(request, joinPoint);
//        log.info("doSignAround==>签名前参数==>" + params);
//        // 供应商的id，验证用户的真实性
//        String appId = params.getString("appId");
//        // 请求发起的时间
//        long timestamp = 0L;
//        try {
//            timestamp = params.getLong("timestamp");
//        } catch (Exception e) {
//            return Response.error(ResponseCodeEnum.E_303);
//        }
//        // 获取配置文件的应用信息
//        List<ApiSecretModel> appList = apiSecretConfig.getAppList();
//        if (appList == null || appList.size() <= 0) {
//            return Response.error(502, "系统未配置活动应用信息");
//        }
//         List<ApiSecretModel> searchAppList = appList.stream().filter(s -> appId.equals(s.getAppId())).collect(Collectors.toList());
//        if (searchAppList == null || searchAppList.size() <= 0) {
//            return Response.error(503, "AppID错误或未进行配置");
//        }
//        // 签名算法生成的签名
//        String sign = params.getString("sign");
//        if (HelperUtils.isNullOrTrimEmpty(appId) || HelperUtils.isNullOrTrimEmpty(timestamp) || HelperUtils.isNullOrTrimEmpty(sign)) {
//            return Response.error(ResponseCodeEnum.E_300);
//        }
//        long minutes = (System.currentTimeMillis() - timestamp) / (1000 * 60);
//        if (minutes > 10L) {
//            return Response.error(ResponseCodeEnum.E_405);
//        }
//        if (!SignUtil.verifySign(params ,searchAppList.get(0).getAppSecret())) {
//            return Response.error(ResponseCodeEnum.E_304);
//        }
//        return joinPoint.proceed();
//    }
//
///**
//     * 获取请求参数
//     * @param request
//     * @param joinPoint
//     * @return
//     */
//
//    private JSONObject getRequestParams(HttpServletRequest request, ProceedingJoinPoint joinPoint) throws JsonProcessingException {
//        JSONObject jsonParams = new JSONObject();
//        if (HttpMethod.POST.name().equalsIgnoreCase(request.getMethod()) ||
//                HttpMethod.PUT.name().equalsIgnoreCase(request.getMethod())) {
//            //获取非 request parameter 中的参数
//            Object[] objects = joinPoint.getArgs();
//            for (Object arg : objects) {
//                if (arg == null) {
//                    break;
//                }
//                String contentType = request.getHeader("Content-Type");
//                if (contentType != null && contentType.contains("application/json")) {
//                    //json 参数
//                    ObjectMapper mapper = new ObjectMapper();
//                    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//                    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
//                    jsonParams = JSONObject.parseObject(mapper.writeValueAsString(arg));
//                }
//            }
//        }
//        //获取 request parameter 中的参数
//        Map<String, String[]> parameterMap = request.getParameterMap();
//        if (parameterMap != null && !parameterMap.isEmpty()) {
//            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
////                if (entry.getValue().length > 0) {
//                String[] arrayValue = entry.getValue();
//                jsonParams.put(entry.getKey(), arrayValue[0]);
////                }
//            }
//        }
//        return jsonParams;
//    }
//}
